﻿using System;
using Styx.Common;
using Styx.CommonBot;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx;
using Styx.WoWInternals.WoWObjects;

namespace PPHelper
{
    public class Main : HBPlugin
    {
        public override string Author { get { return "toNyx"; } }
        public override string Name { get { return "PPHelper"; } }
        public override Version Version { get { return new Version(1, 0, 0); } }
        
        public override bool WantButton { get { return true; } }
        public override string ButtonText { get { return "Settings"; } }
        public override void OnButtonPress() { new SettingsForm().ShowDialog(); }

        public static void Log(string msg) { Logging.Write("[PPHelper]: " + msg); }

        private static readonly LocalPlayer Me = StyxWoW.Me;

        public override void OnEnable()
        {
            // Intercept the SpellCast for Vanish
            Lua.Events.AttachEvent("UNIT_SPELLCAST_SUCCEEDED", Helpers.ClearTargetAfterVanish);

            Log("Plugin enabled");
        }

        public override void OnDisable()
        {
            // Detach event
            Lua.Events.DetachEvent("UNIT_SPELLCAST_SUCCEEDED", Helpers.ClearTargetAfterVanish);

            Log("Plugin disabled");
        }

        public override void Pulse()
        {

            /*
             * -----------------------------------
             * Unstucker
             * -----------------------------------
             */

            if (PPHSettings.Instance.TryUnstuck)
                StuckHandler.CheckForStucks();

            /*
             * -----------------------------------
             * Preparation Optimisation
             * -----------------------------------
             */

            if (PPHSettings.Instance.UsePreparationForVanish)
            {
                if (Me.Combat && Helpers.GetSpellCooldown("Vanish").TotalSeconds > 0)
                {
                    if (SpellManager.CanCast("Preparation"))
                    {
                        Log("Casting preparation to reset Vanish's CD");
                        SpellManager.Cast("Preparation");
                    }
                }
            }

            /*
             * -----------------------------------
             * Use Sprint on Cooldown part
             * -----------------------------------
             */

            if (PPHSettings.Instance.UseSprintOnCooldown)
            {
                if (Helpers.ViableForCast("Sprint"))
                {
                    Log("Casting Sprint");
                    SpellManager.Cast("Sprint");
					StyxWoW.SleepForLagDuration();
                }
                    
            }
        }

        //TEST
        /*
        ObjectManager.GetObjectsOfType<WoWUnit>()
            .Where(unit => unit.IsIndoors && Blacklist.Contains(unit.Guid, BlacklistFlags.All))
            .ForEach(u => Blacklist.Add(u.Guid, BlacklistFlags.All, TimeSpan.FromHours(10)));
        */

    }
}
